package com.hongdee.crm.workorder.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.workorder.service.ITCrmCarWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称:TCrmCarWorkOrderController
 * 类  型: class
 * 描  述: 通过 token取用户信息
 * 路  径:com.hongdee.common.util
 * 作  者: 术洪超
 * 日  期:2017-02-22 15:57
 */

@RestController
@RequestMapping("/crm/workorder")
public class TCrmCarWorkOrderController {

    @Autowired
    private ITCrmCarWorkOrderService itCrmCarWorkOrderService;

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */

    @RequestMapping("/impPage")
    @ResponseBody
    public Map impPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "") String start,
                       @RequestParam(name = "limit", defaultValue = "") String limit
    ) throws Exception {

        Map map = itCrmCarWorkOrderService.impPage(uid, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param id    要确认数据的主键
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/impConfirm")
    @ResponseBody
    public ResultEntity impConfirm(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id)
            throws Exception {
        itCrmCarWorkOrderService.impConfirm(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param uid   要确认数据的主键
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/impConfirms")
    @ResponseBody
    public ResultEntity impConfirms(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody List<Map<Object, String>> data)
            throws Exception {
        itCrmCarWorkOrderService.impConfirms(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/impConfirmAll")
    @ResponseBody
    public ResultEntity impConfirmAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token)
            throws Exception {
        itCrmCarWorkOrderService.impConfirmAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param id    要删除数据的主键
     * @return
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/impDel")
    @ResponseBody
    public ResultEntity impDel(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id", defaultValue = "") String id
    ) throws Exception {
        itCrmCarWorkOrderService.impDel(id);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * 查询工单信息
     */
    @RequestMapping("/work")
    @ResponseBody
    public Map work(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "carId", defaultValue = "") String carId,
                    @RequestParam(name = "vin", defaultValue = "") String vin,
                    @RequestParam(name = "typeCode", defaultValue = "") String typeCode,
                    @RequestParam(name = "typePCode", defaultValue = "") String typePCode,
                    @RequestParam(name = "companyId", defaultValue = "") String companyId

    ) throws Exception {
        Map map = itCrmCarWorkOrderService.workOrderQuery(uid, vin,companyId,typePCode,typeCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入删除
     * @author 术洪超
     */
    @RequestMapping("/impDelAll")
    @ResponseBody
    public ResultEntity impDelAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itCrmCarWorkOrderService.impDelAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 删除多条待导入的数据
     * @author 术洪超
     */
    @RequestMapping("/impDels")
    @ResponseBody
    public ResultEntity impDels(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody List<Map<Object, String>> data) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(data)) {
            throw new Exception("请选择记录！");
        }
        this.itCrmCarWorkOrderService.impDels(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * * @param uid   登录用户id
     *
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 根据VIN查找工单历史项目
     * @author 于扬
     */
    @RequestMapping("/listByVin")
    @ResponseBody
    public ResultEntity appVin(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        ResultEntity res = new ResultEntity();

        res.setMsg("保存成功！");
        res.setDataList(this.itCrmCarWorkOrderService.appVin(vin, uid));
        res.setSuccess(true);
        return res;
    }
}
